package com.example.taobaolianmeng.presenter.impl;

import android.util.Log;

import com.example.taobaolianmeng.model.Api;
import com.example.taobaolianmeng.model.domain.TicketParams;
import com.example.taobaolianmeng.model.domain.TicketResult;
import com.example.taobaolianmeng.presenter.ITicketPresenter;
import com.example.taobaolianmeng.utils.RetrofitManager;
import com.example.taobaolianmeng.utils.UrlUtils;
import com.example.taobaolianmeng.view.ITicketPagerCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TicketPresenterImpl implements ITicketPresenter {
    private static final String TAG = "TicketPresenterImpl";
    private ITicketPagerCallback mViewCallback = null;
    private String mCover =null;
    private TicketResult mTicketResult;

    enum LoadState {
        LOADING,SUCCESS,ERROR,NONE
    }

    private LoadState mCurrentState = LoadState.NONE;

    @Override
    public void getTicket(String title, String url, String cover) {
        this.onTicketLoading();
        this.mCover = cover;
        mCurrentState =LoadState.LOADING;
        String targetUrl = UrlUtils.getTicketUrl(url);
        //获取淘口令
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        Api api = retrofit.create(Api.class);
        TicketParams ticketParams = new TicketParams(targetUrl,title);
        Call<TicketResult> ticket = api.getTicket(ticketParams);
        ticket.enqueue(new Callback<TicketResult>() {

            @Override
            public void onResponse(Call<TicketResult> call, Response<TicketResult> response) {
                int code = response.code();
                Log.d(TAG, "code: "+code);
                if(code == HttpURLConnection.HTTP_OK){
                    mTicketResult = response.body();
                    Log.d(TAG,"result ->"+ mTicketResult.toString());
                    //通知ui跟新
                    onTicketLoadedSuccess();
                }else{
                    onLoadedTicketError();
                    mCurrentState = LoadState.ERROR;
                }
            }

            @Override
            public void onFailure(Call<TicketResult> call, Throwable t) {
                onLoadedTicketError();
            }
        });

    }

    private void onTicketLoadedSuccess() {
        if (mViewCallback != null) {
//            Log.d(TAG,"加载成功");
            mViewCallback.onTicketLoaded(mCover, mTicketResult);
        }else{
            mCurrentState = LoadState.SUCCESS;
        }
    }

    private void onLoadedTicketError() {
        if (mViewCallback != null) {
            mViewCallback.onNetworkError();
        }else{
            mCurrentState = LoadState.ERROR;
        }
    }

    @Override
    public void unregisterViewCallback(ITicketPagerCallback callBack) {
        this.mViewCallback = null;
    }

    private void onTicketLoading() {
        if (mViewCallback != null) {
            mViewCallback.onLoading();
        }else{
            mCurrentState = LoadState.LOADING;
        }
    }

    @Override
    public void registerViewCallback(ITicketPagerCallback callBack) {
        this.mViewCallback = callBack;
        if(mCurrentState != LoadState.NONE){
            //说明状态改变了更新ui
            if(mCurrentState ==LoadState.SUCCESS){
                onTicketLoadedSuccess();
            }else if(mCurrentState ==LoadState.ERROR){
                onLoadedTicketError();
            }else if(mCurrentState == LoadState.LOADING){
                onTicketLoading();
            }
        }
    }
}
